﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using DLL;

public partial class ryPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //绑定数据源
            BindData();
        }
        //验证列表视图是否有数据
        viewIsNoData();
    }

    //绑定数据源
    private void BindData()
    {
        gvRY.DataSource = ry_BLL.GetSource();
        gvRY.DataBind();
    }   

    //提交
    protected void btnRefur_Click(object sender, EventArgs e)
    {
        //验证密码是否一致
        if (txtPwd.Text != txtPwd.Text)
        {
            ShowMessage("两次输入密码不一致！");
            return;
        }

        //判断院系名称是否为空
        if (txtName.Text.Length == 0 || txtPwd.Text.Length == 0)
        {
            ShowMessage("请输入职工名称或密码！");
            return;
        }
        //创建新的数据对象
        ZG newZG = new ZG();
        //为数据项赋值 
        newZG.zgName = txtName.Text;
        newZG.zgPwd = txtPwd.Text;
        newZG.bWork = dropWroking.SelectedValue == "0" ? false : true;
        newZG.bMana = dropManager.SelectedValue == "0" ? false : true;
        newZG.zgSex = dropSex.SelectedValue == "0" ? false : true;
        //验证操作类型
        switch (btnRefur.Text)
        {
            case "添加":
                //添加职工
                bool bl = ry_BLL.Add(newZG);
                if (bl)
                {
                    ShowMessage("添加成功！");
                    //刷新当前页
                    Response.Redirect("ryPage.aspx");
                }
                else
                    ShowMessage("添加失败！");
                break;
            case "更新":
                //更新职工
                bool b = ry_BLL.Update(updateIndex, newZG);
                if (b)
                {
                    ShowMessage("更新成功！");
                    //刷新当前页
                    Response.Redirect("ryPage.aspx");
                }
                else
                    ShowMessage("更新失败！");
                btnRefur.Text = "添加";
                break;
        }
    }

    //选择行
    private static string updateIndex = "";
    protected void gvRY_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        //获取当前选中行
        GridViewRow row=gvRY.Rows[e.NewSelectedIndex];
        //从当前选中行中取值 
        updateIndex = gvRY.DataKeys[e.NewSelectedIndex][0].ToString();
        txtName.Text = row.Cells[1].Text;
        txtPwd.Text = row.Cells[3].Text;
        dropSex.ClearSelection();
        dropManager.ClearSelection();
        dropWroking.ClearSelection();
        dropSex.Items.FindByText(row.Cells[2].Text).Selected=true;
        dropManager.Items.FindByText(row.Cells[4].Text).Selected = true;
        dropWroking.Items.FindByText(row.Cells[5].Text).Selected = true;
        btnRefur.Text = "更新";
    }
    //取消
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        //重置控件值
        dropSex.ClearSelection();
        dropManager.ClearSelection();
        dropWroking.ClearSelection();
        txtName.Text = txtPwd.Text = "";
        btnRefur.Text = "添加";
    }

    //翻页
    protected void gvRY_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvRY.PageIndex = e.NewPageIndex;
        BindData();
    }
    
    //删除
    protected void gvRY_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        ry_BLL.Delete(gvRY.DataKeys[e.RowIndex][0].ToString());
        ShowMessage("删除成功！");
        BindData();
    }

    #region 消息
    /// <summary>
    /// 如果GridView中没有数据则显示提示信息
    /// </summary>
    protected void viewIsNoData()
    {
        if (gvRY.Rows.Count == 0)
        {
            RegisterStartupScript("prompt", "<script type='text/javascript'>document.getElementById('divBrowse').innerHTML='(尚未添加相关数据)'</script>");
        }
    }
    /// <summary>
    /// 显示消息
    /// </summary>
    /// <param name="msg">消息内容</param>
    protected void ShowMessage(string msg)
    {
        //注册脚本显示消息
        RegisterStartupScript("msg", "<script type='text/javascript' language='javascript'>alert('" + msg + "')</script>");
    }
    #endregion    

}
